home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / CRCUTIL.ZIP / CRCUTIL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-22  |  5.7 KB  |  190 lines

  1. /***********************************************************************
  2. * CRC utility routines for general 16 and 32 bit CRCs.
  3. *
  4. * 1990  Gary P. Mussar
  5. * This code is released to the public domain. There are no restrictions,
  6. * however, acknowledging the author by keeping this comment around
  7. * would be appreciated.
  8. ***********************************************************************/
  9. #include "crcutil.h"
  10.  
  11. /***********************************************************************
  12. * Utilities for fast CRC using table lookup
  13. *
  14. * CRC calculations are performed one byte at a time using a table lookup
  15. * mechanism.  Two routines are provided: one to initialize the CRC table;
  16. * and one to perform the CRC calculation over an array of bytes.
  17. *
  18. * A CRC is the remainder produced by dividing a generator polynomial into
  19. * a data polynomial using binary arthimetic (XORs). The data polynomial
  20. * is formed by using each bit of the data as a coefficient of a term in
  21. * the polynomial. These utilities assume the data communications ordering
  22. * of bits for the data polynomial, ie. the LSB of the first byte of data
  23. * is the coefficient of the highest term of the polynomial, etc..
  24. *
  25. * I_CRCxx  -  Initialize the 256 entry CRC lookup table based on the
  26. *             specified generator polynomial.
  27. * Input:
  28. *    Table[256]     - Lookup table
  29. *    *GenPolynomial - Pointer to generator polynomial
  30. *
  31. * F_CRCxx  -  Calculate CRC over an array of characters using fast
  32. *             table lookup.
  33. * Input:
  34. *    Table[256]    - Lookup table
  35. *    *CRC          - Pointer to the variable containing the result of
  36. *                    CRC calculations of previous characters. The CRC
  37. *                    variable must be initialized to a known value
  38. *                    before the first call to this routine.
  39. *    *dataptr      - Pointer to array of characters to be included in
  40. *                    the CRC calculation.
  41. *    count         - Number of characters in the array.
  42. ***********************************************************************/
  43.  
  44. void I_CRC16(Table, GenPolynomial)
  45.    CRC16 Table[256];
  46.    CRC16 *GenPolynomial;
  47. {
  48.    int i;
  49.    CRC16 crc, poly;
  50.  
  51.    for (poly=*GenPolynomial, i=0; i<256; i++)
  52.    {
  53.       crc = (CRC16) i;
  54.       crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  55.       crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  56.       crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  57.       crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  58.       crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  59.       crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  60.       crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  61.       crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  62.  
  63.       Table[i] = crc;
  64.    }
  65. }
  66.  
  67. void I_CRC32(Table, GenPolynomial)
  68.    CRC32 Table[256];
  69.    CRC32 *GenPolynomial;
  70. {
  71.    int i;
  72.    CRC32 crc, poly;
  73.  
  74.    for (poly=*GenPolynomial, i=0; i<256; i++)
  75.    {
  76.       crc = (CRC32) i;
  77.       crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  78.       crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  79.       crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  80.       crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  81.       crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  82.       crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  83.       crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  84.       crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  85.  
  86.       Table[i] = crc;
  87.    }
  88. }
  89.  
  90. void F_CRC16(Table, CRC, dataptr, count)
  91.    CRC16 Table[256];
  92.    CRC16 *CRC;
  93.    void *dataptr;
  94.    unsigned int count;
  95. {
  96.    CRC16 temp_crc;
  97.    unsigned char *p;
  98.  
  99.    p = (unsigned char *)dataptr;
  100.  
  101.    for (temp_crc = *CRC; count; count--)
  102.    {
  103.       temp_crc = (temp_crc >> 8) ^ Table[(temp_crc & 0xff) ^ *p++];
  104.    }
  105.  
  106.    *CRC = temp_crc;
  107. }
  108.  
  109. void F_CRC32(Table, CRC, dataptr, count)
  110.    CRC32 Table[256];
  111.    CRC32 *CRC;
  112.    void  *dataptr;
  113.    unsigned int count;
  114. {
  115.    CRC32 temp_crc;
  116.    unsigned char *p;
  117.  
  118.    p = (unsigned char *)dataptr;
  119.  
  120.    for (temp_crc = *CRC; count; count--)
  121.    {
  122.       temp_crc = (temp_crc >> 8) ^ Table[(temp_crc & 0xff) ^ *p++];
  123.    }
  124.  
  125.    *CRC = temp_crc;
  126. }
  127.  
  128. /***********************************************************************
  129. * Utility CRC using slower, smaller non-table lookup method
  130. *
  131. * S_CRCxx  -  Calculate CRC over an array of characters using slower but
  132. *             smaller non-table lookup method.
  133. * Input:
  134. *    GenPolynomial - Generator polynomial
  135. *    *CRC          - Pointer to the variable containing the result of
  136. *                    CRC calculations of previous characters. The CRC
  137. *                    variable must be initialized to a known value
  138. *                    before the first call to this routine.
  139. *    *dataptr      - Pointer to array of characters to be included in
  140. *                    the CRC calculation.
  141. *    count         - Number of characters in the array.
  142. ***********************************************************************/
  143. void S_CRC16(GenPolynomial, CRC, dataptr, count)
  144.    CRC16 *GenPolynomial;
  145.    CRC16 *CRC;
  146.    void *dataptr;
  147.    unsigned int count;
  148. {
  149.    int i;
  150.    CRC16 temp_crc, poly;
  151.    unsigned char *p;
  152.  
  153.    p = (unsigned char *)dataptr;
  154.  
  155.    for (poly=*GenPolynomial, temp_crc = *CRC; count; count--)
  156.    {
  157.       temp_crc ^= *p++;
  158.       for (i=0; i<8; i++)
  159.       {
  160.          temp_crc = (temp_crc >> 1) ^ ((temp_crc & 1) ? poly : 0);
  161.       }
  162.    }
  163.  
  164.    *CRC = temp_crc;
  165. }
  166.  
  167. void S_CRC32(GenPolynomial, CRC, dataptr, count)
  168.    CRC32 *GenPolynomial;
  169.    CRC32 *CRC;
  170.    void *dataptr;
  171.    unsigned int count;
  172. {
  173.    int i;
  174.    CRC32 temp_crc, poly;
  175.    unsigned char *p;
  176.  
  177.    p = (unsigned char *)dataptr;
  178.  
  179.    for (poly=*GenPolynomial, temp_crc = *CRC; count; count--)
  180.    {
  181.       temp_crc ^= *p++;
  182.       for (i=0; i<8; i++)
  183.       {
  184.          temp_crc = (temp_crc >> 1) ^ ((temp_crc & 1) ? poly : 0);
  185.       }
  186.    }
  187.  
  188.    *CRC = temp_crc;
  189. }
  190.